/**
 * 状态map
 */
let statusMap = {
    "0": "draft",
    "1": "review",
    "2": "approve",
    "3": "complete"
}

/**
 * 查询采购订单列表
 * 
 * @param {*} queryMode 查询mode，1为todo 2为related
 */
function queryProcurementOrders(queryMode) {
    $.ajax({
        type: 'GET',
        contentType: "application/json;charset=UTF-8",
        url: "/service/workflow/procurement/queryProcurementOrders/" + queryMode,
        headers: {
            Authorization: JwtTokenUtils.getBearerHeader()
        },
        success: function (data) {
            let listDiv;
            if (queryMode == "TODOS") {
                listDiv = $(".procurement-list-component .procurement-list-panel .todo-list");
            }
            else {
                listDiv = $(".procurement-list-component .procurement-list-panel .related-list");
            }
            listDiv.empty();
            for (let i = 0; i < data.length; i++) {
                let procurement = data[i];
                let procurementPanel = $(".procurement-list-component .templates .procurement-panel").clone();
                let procurementCodeA = procurementPanel.find(".panel-heading .panel-title a");
                procurementCodeA.text(procurement.code);
                procurementCodeA.attr("href", "procurement.html?id=" + procurement.id);
                procurementPanel.find(".panel-body .id-value").text(procurement.id);
                procurementPanel.find(".panel-body .code-value").text(procurement.code);
                procurementPanel.find(".panel-body .name-value").text(procurement.name);
                procurementPanel.find(".panel-body .description-value").text(procurement.description);
                procurementPanel.find(".panel-body .currentHandler-value").text(procurement.currentHandler);
                procurementPanel.find(".panel-body .creator-value").text(procurement.creator);
                procurementPanel.find(".panel-body .submitter-value").text(procurement.submitter);
                procurementPanel.find(".panel-body .submitter-value").text(procurement.submitter);
                procurementPanel.find(".panel-body .reviewer-value").text(procurement.reviewer);
                procurementPanel.find(".panel-body .approver-value").text(procurement.approver);
                procurementPanel.find(".panel-body .status-value").text(statusMap[procurement.status]);
                listDiv.append(procurementPanel);
            }
        }
    });
}


/**
 * 页面启动完成事件
 *
 */
$(document).ready(function () {
    queryProcurementOrders("TODOS");
    queryProcurementOrders("RELATED");

    $(".procurement-list-component .export-procurement-orders").click(function() {
        let url = "/service/workflow/procurement/exportProcurementOrders";
        IntegrationUtils.downloadFile(url, "GET");
    })
});